package emp.dao;

import emp.config.DataSourceConfig;
import emp.config.PaginationService;
import emp.entity.PageInfo;
import emp.entity.SearchCriteria;
import emp.entity.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author 叶枫
 * @date 2024/12/18.
 */


public class UserDao {
private static DataSourceConfig dataSourceConfig;

private   JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSourceConfig.dataSource());

    // 登录
    public User login(String username, String password) {
        String sql = "select * from user where username = ? and password = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username, password);
        } catch (Exception e) {
            return null;
        }
    }

    //检查用户名是否已存在

    public boolean checkUserExists(String username) {
        String sql = "select * from user where username = ?";
        try {
            User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username);
            return user != null;
        } catch (EmptyResultDataAccessException e) {
            // 用户不存在的情况
            return false;
        } catch (Exception e) {
            // 其他异常情况
            return false;
        }

    }
    //注册
    public boolean register(User user) {
        String sql = "insert into user(username,password,role) values(?,?,?)";
        int update = jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getRole());
        return update > 0;
    }

    //添加用户
    public boolean add(User user) {
        String sql = "insert into user(username,password,role) values(?,?,?)";
        int update = jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getRole());
        return update > 0;
    }
    public boolean delete(int userId) {
        String sql = "delete from user where user_id = ?";
        int update = jdbcTemplate.update(sql, userId);
        return update > 0;
    }
    public boolean update(User user) {
        String sql = "update user set username = ?, password = ?, role = ? where user_id = ?";
        int update = jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getRole(), user.getUserId());
        return update > 0;
    }
    public User getById(int userId) {
        String sql = "select * from user where user_id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), userId);
    }
    public PageInfo<User> listByPage(int pageNumber, int pageSize ,String username){
        String baseSql = "select * from user where 1=1";
        if(username != null && !"".equals(username)){
            baseSql += " and username like '%" + username + "%'";
        }
        PaginationService<User> paginationService = new PaginationService<>();
        return paginationService.getPage(baseSql, new SearchCriteria(), new BeanPropertyRowMapper<>(User.class), pageNumber, pageSize);
    }

}
